Expanding Nested Lists

This technique uses a nested list, some simple javascript, and CSS to create a expanding "tree" effect. It seems to work fairly well, but there are some issues, as I discuss below. Mostly, I haven't had the time to explore all of the issues.

Feel free to send feedback to karlnelson@earthlink.net.

The JavaScript

The HTML

The HTML looks worse than it is. Basically, each item in the list has its own id, which is used to open an close specific portions of the list. The leaf nodes (non-opening and closing) don't really need ids. Every time I've used this technique in a production site, I've automatically generated the HTML using either a server side scripting language (PHP) and values pulled from a database, or XSLT run over an XML document that contains the "tree."

The CSS

Issues

Horizontal Spacing of Text. Ideally, you'd want the first character in each level to line up. This could pretty easily be achieved by using a spacer image (either transparent or a circle) for leaf nodes. Some CSS might work, too.

Accessibility. I don't have alt attributes set, but I imagine it wouldn't be too hard to set up some javascript to change the alts dynamically. I have no idea what this would look like in a screen reader or other piece of accessible technology.

More on Accessibility...

Michele Ide-Smith of Green Cathedral sent me a note about accessibility and this technique:

I have tested your code with Connect Outloud which is a cut down version of Jaws for Windows (a popular screen reader) and it performed very well.
A user with a screen reader is read the links one by one as if the menu is completely expanded. A good way to make it more usable for anyone with a screen reader would be to add hidden navigation controls e.g. a link around a transparent 1 pixel gif which allows a user to skip between the top level menu options. So rather than having to be read the entire menu, a user would hear "item 1" then "skip to item 2" rather than "item 1, item 1.1, item 1.2, item 2".
You could also add the access key tag to bind links to access keys e.g. a link to item 1 would be <a href="item1.html" accesskey="1">item 1</a>.
Hope that helps. I am very interested in finding the best way to create accessible navigation that is neat and works well across all browsers. This approach certainly seems to be the best I've come across so far.

I hope Michele's suggestions are useful to others. If you have anything to add, please contact me at karlnelson@earthlink.net.

Version 1.0, 3/16/04, Karl Nelson

Update (11/6/03): I found a The List posting from last year that covers pretty much the same ground.

Update (2/27/04): I added some CSS that helps vertically align the images a bit better: li img { vertical-align: middle; }. Addressing another issue, I removed the href="#" from the links. With the href="#", the browser will move up to the top of the page whenever the link is clicked; without, the list can expand and contract without moving the page. More details on this can be found on J. Blustein's page on nested lists.

Update (3/16/04): I added Michele Ide-Smith's note on accessibility. I also put the source code in text boxes for easy cut-n-pasting.


Visit my weblog: The Information Management Weblog, maintained by Karl Nelson, covers a variety of information related topics, including content management, knowledge management, web development, and anything else that looks interesting.